#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 2e5 + 10;
int a[N], b[N];
vector<int> G[N];
int in[N];

void solve() {
	int n, m; cin >> n >> m;
	for (int i = 1; i <= n; i++) G[i].clear(), in[i] = 0;
	while (m--) {
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
			if (i >= 3) G[a[i - 1]].push_back(a[i]), in[a[i]]++;
		}
	}
	queue<int> q;
	for (int i = 1; i <= n; i++) {
		if (in[i] == 0) q.push(i);
	}
	int cnt = 0;
	while (q.size()) {
		int u = q.front(); q.pop();
		cnt++;
		for (int v : G[u]) {
			in[v]--;
			if (in[v] == 0) q.push(v);
		}
	}
	if (cnt == n) cout << "YES" << endl;
	else cout << "NO" << endl;
} 


int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}